#include "CacheCheck.h"

CacheCheck::CacheCheck(QObject *parent):CacheBase(parent)
{

}

void CacheCheck::checkDataBase(qlonglong storageDuration, qlonglong cacheRows, qlonglong uploadTimes)
{
    //	UploadInfo
    checkTime(s_uploadInfoTableName,storageDuration);
    checkRows(s_uploadInfoTableName,s_idKey,cacheRows);
    //	ReuploadInfo
    checkTime(s_reuploadInfoTableName,storageDuration);
    checkTimes(s_reuploadInfoTableName,uploadTimes);
    checkRows(s_reuploadInfoTableName,s_idKey,cacheRows);
    //	Tid
    checkRows(s_tidInfoTableName,s_infoKey,cacheRows);
}
void CacheCheck::checkTime(QString tableName, qlonglong storageDuration)
{
    QList<QString> key;
    QList<QList<QVariant>> value;
    QList<qlonglong> id;
    QList<QString> status;
    QList<QString> deleteKey;
    QList<QList<QVariant>> deleteId;
    qlonglong nowTime = QDateTime::currentDateTime().toMSecsSinceEpoch()/1000;
    key<<s_idKey<<s_stateKey;
    deleteKey << s_idKey;
    int32_t checkInfoNum = s_checkInfoNum;
    value = m_dataBaseInterface->readRow(tableName,key,checkInfoNum);
    for(int32_t indexNum = 0; indexNum < value.size(); indexNum++)
    {
        for(int32_t indexKey=0;indexKey < key.size(); indexKey++)
        {
            if(key.at(indexKey) == s_idKey)
            {
                id << value.at(indexNum).at(indexKey).toLongLong();
            }
            else if(key.at(indexKey) == s_stateKey)
            {
                QString tempStr;
                decryptSimple(value.at(indexNum).at(indexKey).toString(),tempStr);
                status << tempStr;
            }
        }
    }
    for(int32_t indexNum=0;indexNum < id.size(); indexNum++)
    {
        QList<QVariant> tempDeleteId;
        QJsonDocument tempJsonDoc = QJsonDocument::fromJson(status.at(indexNum).toUtf8());
        if(!tempJsonDoc.isObject()
                ||	!tempJsonDoc.object().contains(s_createTimeStampKey)
                ||	nowTime - tempJsonDoc.object()[s_createTimeStampKey].toVariant().toLongLong() > storageDuration)
        {
            tempDeleteId << id.at(indexNum);
            deleteId << tempDeleteId;
        }
    }
    qInfo()<<"CacheCheck::checkTime:"<<"tableName="<<tableName<<" storageDuration="<< storageDuration<<" deleteKey="<<deleteKey<<" deleteId="<<deleteId;
    m_dataBaseInterface->deleteRow(tableName,deleteKey,deleteId);

}

void CacheCheck::checkTimes(QString tableName, qlonglong uploadTimes)
{
    QList<QString> key;
    QList<QList<QVariant>> value;
    QList<qlonglong> id;
    QList<QString> status;
    QList<QString> deleteKey;
    QList<QList<QVariant>> deleteId;
    key<<s_idKey<<s_stateKey;
    deleteKey << s_idKey;
    int32_t checkInfoNum = s_checkInfoNum;
    value = m_dataBaseInterface->readRow(tableName,key,checkInfoNum);
    for(int32_t indexNum = 0; indexNum < value.size(); indexNum++)
    {
        for(int32_t indexKey=0;indexKey < key.size(); indexKey++)
        {
            if(key.at(indexKey) == s_idKey)
            {
                id << value.at(indexNum).at(indexKey).toLongLong();
            }
            else if(key.at(indexKey) == s_stateKey)
            {
                QString tempStr;
                decryptSimple(value.at(indexNum).at(indexKey).toString(),tempStr);
                status << tempStr;
            }
        }
    }
    for(int32_t indexNum=0;indexNum < id.size(); indexNum++)
    {
        QList<QVariant> tempDeleteId;
        QJsonDocument tempJsonDoc = QJsonDocument::fromJson(status.at(indexNum).toUtf8());
        if(!tempJsonDoc.isObject()
                ||!tempJsonDoc.object().contains(s_uploadTimesKey)
                ||tempJsonDoc.object()[s_uploadTimesKey].toVariant().toLongLong() > uploadTimes
                )
        {
            tempDeleteId << id.at(indexNum);
            deleteId << tempDeleteId;
        }
        if(tempJsonDoc.isObject()
                &&tempJsonDoc.object().contains(s_uploadTimesKey)
                )
        {
           qDebug() <<"CacheCheck::checkTimes:tableName"<<tableName<<" index="<<indexNum<<" times="<<tempJsonDoc.object();
        }
    }
    qInfo()<<"CacheCheck::checkTimes:"<<"tableName="<<tableName<<" uploadTimes="<<uploadTimes<<" deleteKey="<<deleteKey<<" deleteId="<<deleteId;
    m_dataBaseInterface->deleteRow(tableName,deleteKey,deleteId);

}

void CacheCheck::checkRows(QString tableName,QString key,qlonglong cacheRows)
{
    bool state;
    state = m_dataBaseInterface->deleteMaxRow(tableName,key,cacheRows);

    qInfo()<<"CacheCheck::checkRows: tableName="<< tableName<<" cacheRows= "<<cacheRows;
}
